﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;

namespace mamnontuoitho7.backend
{
    public partial class MenuCreate : System.Web.UI.Page
    {
        private Article _article = new Article();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionArticle).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (!IsPostBack)
                    {
                        ckeContent.Text = ConstantClass._tableMENU;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void btArticleCreate_Click(object sender, EventArgs e)
        {
            try
            {
                String photoName = UploadImage();
                bool checkInsertArticle = _article.ArticleCreateBySP("1", Session["memberID"].ToString(), txtTitleName.Text, txtDescription.Text, "0", "1", Session["username"].ToString(), "11", "", ckeContent.Text, photoName);
                if (checkInsertArticle)
                {
                    String script1 = "<script language=JavaScript>alert('Tạo bài viết thành công'); window.location = '../backend/MenuCreate.aspx';</script>";
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script1);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }



        private string UploadImage()
        {

            if (!string.IsNullOrEmpty(fulPhoto.FileName))
            {
                if (fulPhoto.FileBytes.Count() / 1024 > 6144)
                {
                    String script = "<script language=JavaScript>alert('Dung lượng hình ảnh phải dưới 6mb')</script>";
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script);
                }
                string fullname = fulPhoto.FileName;
                string nameString = fullname.Substring(0, fullname.LastIndexOf('.'));

                string fileExtension = fullname.Substring(fullname.LastIndexOf('.') + 1, fullname.Length - fullname.LastIndexOf('.') - 1).ToLower();
                //   string fileExtension = splitFileName[splitFileName.Length - 1].Trim().ToLower();

                if (fileExtension == "jpg" || fileExtension == "gif" || fileExtension == "jpeg" || fileExtension == "png" || fileExtension == "bmp" || fileExtension == "tiff")
                {
                    string FilePath = Request.PhysicalApplicationPath + "images/article/";
                    string code = txtTitleName.Text;
                    string name = txtTitleName.Text;
                    ThumbnailClass thumb = new ThumbnailClass();
                    string filename = thumb.UploadImage(fulPhoto, FilePath, code, name, 150, true, true);
                    return filename;
                }
                else
                {
                    String script = "<script language=JavaScript>alert('Chỉ hỗ trợ những file: jpg, jpeg, gif, png, bmp, tiff')</script>";
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script);
                }
            }

            return "";
        }
    }
}